package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.result.Result;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
@Slf4j
@Api(tags = "用户相关接口")
@RequiredArgsConstructor
public class UserController {

  private final IUserService userService;

  /**
   * 新增用户
   *
   * @param userFormDTO
   * @return
   */
  @PostMapping
  @ApiOperation("新增用户")
  public Result saveUser(@RequestBody UserFormDTO userFormDTO) {
    log.info("新增用户,{}", userFormDTO);

    // 1.把DTO拷贝到PO
    User user = BeanUtil.copyProperties(userFormDTO, User.class);

    // 2.新增
    userService.save(user);

    return Result.success();
  }

  /**
   * 删除用户接口
   *
   * @param id
   * @return
   */
  @DeleteMapping("/{id}")
  @ApiOperation("删除用户接口")
  public Result deleteById(@ApiParam("用户id") @PathVariable("id") Long id) {
    log.info("删除用户接口, {}", id);

    userService.removeById(id);

    return Result.success();
  }

  /**
   * 根据id查询用户接口
   *
   * @param id
   * @return
   */
  @GetMapping("/{id}")
  @ApiOperation("根据id查询用户接口")
  public Result<UserVO> getUserInfoById(@ApiParam("用户id") @PathVariable("id") Long id) {
    log.info("根据id查询用户接口, {}", id);

    // 2.把PO拷贝到VO
    UserVO userVO = userService.queryUserAndAddressById(id);

    return Result.success(userVO);
  }

  /**
   * 根据ids批量查询用户接口
   *
   * @param ids
   * @return
   */
  @GetMapping
  @ApiOperation("根据ids批量查询用户接口")
  public Result<List<UserVO>> getUsersInfoByIds(@ApiParam("用户id集合") @RequestParam("ids") List<Long> ids) {
    log.info("根据ids批量查询用户接口, {}", ids);

    // 1.查询用户
    List<UserVO> userVOS = userService.queryUserAndAddressByIds(ids);

    return Result.success(userVOS);
  }

  /**
   * 扣除用户金额接口
   *
   * @param id
   * @param money
   * @return
   */
  @GetMapping("/{id}/deduction/{money}")
  @ApiOperation("扣除用户金额接口")
  public Result deductBalance(
    @ApiParam("用户id") @PathVariable("id") Long id,
    @ApiParam("扣减的金额") @PathVariable("money") Integer money) {
    log.info("扣除用户金额接口, id:{}, money:{}", id, money);

    userService.deductBalance(id, money);

    return Result.success();
  }

  /**
   * 根据复杂条件查询用户接口
   * @param userQuery
   * @return
   */
  @GetMapping("/list")
  @ApiOperation("根据复杂条件查询用户接口")
  public Result<List<UserVO>> queryUsers(UserQuery userQuery) {
    log.info("根据复杂条件查询用户接口, {}", userQuery);
    // 1.查询用户
    List<User> users = userService.queryUsers(
      userQuery.getName(),
      userQuery.getStatus(),
      userQuery.getMinBalance(),
      userQuery.getMaxBalance());
    // 2.把PO拷贝到VO
    List<UserVO> userVOList = BeanUtil.copyToList(users, UserVO.class);
    return Result.success(userVOList);
  }

  /**
   * 根据复杂条件分页查询用户接口
   * @param userQuery
   * @return
   */
  @GetMapping("/page")
  @ApiOperation("根据复杂条件分页查询用户接口")
  public Result<PageDTO<UserVO>> queryUsersPage(UserQuery userQuery) {
    log.info("根据复杂条件分页查询用户接口, {}", userQuery);

    PageDTO<UserVO> userPage = userService.queryUserPage(userQuery);

    return Result.success(userPage);
  }
}
